package com.wanhe.base.util;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloadQueueSet;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
import com.liulishuo.filedownloader.services.DownloadMgrInitialParams;
import com.wanhe.base.BaseApplication;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanchx on 2017/7/13.
 */

public class FileDownLoadUtil {

	public static void initFileDownloader() {
		FileDownloader.init(BaseApplication.context,
				new DownloadMgrInitialParams.InitCustomMaker()
						.connectionCreator(new FileDownloadUrlConnection.Creator(
								new FileDownloadUrlConnection.Configuration().connectTimeout(15_000) // set
																										// connection
																										// timeout.
										.readTimeout(15_000) // set read
																// timeout.
										.proxy(Proxy.NO_PROXY) // set proxy
				)));
	}

	public static void downloader(String srcUrl, String path, FileDownloadListener parallelTarget) {
		FileDownloader.getImpl().create(srcUrl).setPath(path).setListener(parallelTarget).start();
	}

	/**
	 *
	 * @param URL
	 *            ip+prot
	 * @param nameList
	 *            文件名称
	 * @param pathList
	 *            本地路径列表
	 * @param parallelTarget
	 */
	public static void downloaderList(String URL, List<String> nameList, List<String> pathList,
			FileDownloadListener parallelTarget) {


		List<BaseDownloadTask> taskList = new ArrayList<>();
		if (nameList != null && nameList.size() > 0) {
			for (int i = 0; i < nameList.size(); i++) {

				taskList.add(FileDownloader.getImpl().create(URL + nameList.get(i))
						.setPath(pathList.get(i)).setTag(i));

			}
		}
		new FileDownloadQueueSet(parallelTarget).setCallbackProgressTimes(1)
				.downloadTogether(taskList).start();
	}

}
